package leetcode;
import java.util.Stack;

/**
 * 牛客网测试/leetcode
 * 计算逆波兰式的值
 * @author Administrator
 *
 */
public class 计算逆波兰式的值 {

	public static void main(String[] args) {
		String[] input = new String[] { "0", "3", "/" };
		System.out.println(evalRPN(input));
	}

	public static int evalRPN(String[] tokens) {
		Stack<Integer> stack = new Stack<>();
		for (int i = 0; i < tokens.length; i++) {
			if (tokens[i].equals("+")) {
				int num1 = stack.pop();
				int num2 = stack.pop();
				int resule = num1 + num2;
				stack.push(resule);
			} else if (tokens[i].equals("-")) {
				int num1 = stack.pop();
				int num2 = stack.pop();
				int resule = num2 - num1;
				stack.push(resule);
			} else if (tokens[i].equals("*")) {
				int num1 = stack.pop();
				int num2 = stack.pop();
				int resule = num1 * num2;
				stack.push(resule);
			} else if (tokens[i].equals("/")) {
				int num1 = stack.pop();
				int num2 = stack.pop();
				int resule = num2 / num1;
				stack.push(resule);
			} else {
				stack.push(Integer.valueOf(tokens[i]));
			}

		}
		return stack.pop();
	}
}
